package com.woniuxy.woniuhis.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woniuxy.woniuhis.entity.Cash;
import com.woniuxy.woniuhis.entity.PayIn;
import com.woniuxy.woniuhis.exception.CashServiceException;
import com.woniuxy.woniuhis.mapper.CashMapper;
import com.woniuxy.woniuhis.mapper.PayInMapper;
import com.woniuxy.woniuhis.service.CashService;
@Service
public class CashServiceImpl implements CashService{
	@Autowired
	CashMapper cashMapper;
	@Autowired
	PayInMapper payInMapper;
	@Override
	//添加资金
	public void addCash(Cash cash) {
		try {
			cashMapper.insertCash(cash);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CashServiceException();
		}	
	}

	@Override
	//删除资金
	public void delCashById(String cashId) {
		try {
			cashMapper.deleteCashById(cashId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CashServiceException();
		}			
	}

	@Override
	//修改资金
	public void modifyCash(Cash cash) {
		try {
			cashMapper.updateCash(cash);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CashServiceException();
		}			
	}
	
	@Override
	//按照id查询资金
	public Cash findCashById(String cashId) {
		try {
			Cash cash = cashMapper.selectCashById(cashId);
			return cash;
		} catch (Exception e) {
			e.printStackTrace();
			throw new CashServiceException();
		}			
	}
	
	@Override
	//按条件查找资金
	public List<Cash> findCash(Cash cash) {
		try {
			List<Cash> cashs = cashMapper.selectCash(cash);
			return cashs;
		} catch (Exception e) {
			e.printStackTrace();
			throw new CashServiceException();
		}			
	}

	@Override
	//查询医院总资金
	public BigDecimal findAllCash() {
		try {
			BigDecimal allCash = cashMapper.selectAllCash();
			return allCash;
		} catch (Exception e) {
			e.printStackTrace();
			throw new CashServiceException();
		}			
	}

	@Override
	//按照日期和部门查询收入
	public BigDecimal findCashByDate(PayIn payIn,Date beginDate,Date endDate) {
		try {
			List<PayIn> payIns = payInMapper.selectPayIn(payIn, beginDate, endDate);
			BigDecimal total = new BigDecimal(0);
			for(PayIn pay:payIns) {
				BigDecimal payInMoney = pay.getPayInMoney();
				total=total.add(payInMoney);
			}
			return total;
		} catch (Exception e) {
			e.printStackTrace();
			throw new CashServiceException();
		}			
	}
}
